package com.pinda.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.pinda.oms.entity.PdOrder;
import com.pinda.oms.entity.PdOrderCargo;
import com.pinda.service.IPdOrderCargoService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;


/**
 * <p>
 * 货品总重量  前端控制器
 * </p>
 *
 * @author lc
 * @since 2023-06-14
 */
@RestController
@RequestMapping("/pd-order-cargo")
public class PdOrderCargoController {

    @Resource
    private IPdOrderCargoService iPdOrderCargo;
    //根据order_id查询货物
    @PostMapping("/findOrderCargoOne")
    public List<PdOrderCargo> findOrderCargoOne(@RequestBody PdOrder pdOrder){
        System.out.println(pdOrder.getId());
        QueryWrapper<PdOrderCargo> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("order_id",pdOrder.getId());
        List<PdOrderCargo> pdOrderCargo=iPdOrderCargo.list(queryWrapper);
        System.out.println(pdOrderCargo);
        return pdOrderCargo;
    }

    /**
     * 批量查询货物信息表
     * @param orderIds
     * @return
     */
    @GetMapping("/list")
    public List<PdOrderCargo> list(@RequestParam(name = "orderIds", required = false) List<String> orderIds) {
        LambdaQueryWrapper<PdOrderCargo> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(!CollectionUtils.isEmpty(orderIds), PdOrderCargo::getOrderId, orderIds);

        return iPdOrderCargo.list(wrapper).stream().map(orderCargo -> {
            PdOrderCargo cargoDto = new PdOrderCargo();
            BeanUtils.copyProperties(orderCargo, cargoDto);
            return cargoDto;
        }).collect(Collectors.toList());
    }

    @Resource
    private IPdOrderCargoService iPdOrderCargoService;
    @PostMapping("saveOrderCargo")
    public boolean saveOrderCargo(@RequestBody PdOrderCargo pdOrderCargo){
        pdOrderCargo.setCargoValue(BigDecimal.valueOf(0.1));
        pdOrderCargo.setVolume(BigDecimal.valueOf(0.1));
        pdOrderCargo.setTotalVolume(BigDecimal.valueOf(0.1));
        return this.iPdOrderCargoService.save(pdOrderCargo);
    }

    /**
     * 根据订单id修改货物信息
     * @param
     * @return
     */
    @PostMapping("updOrderTypeTransportOrder/{orderId}/{substring}")
    public boolean updOrderTypeTransportOrder(@PathVariable("orderId") String orderId,@PathVariable("substring") String substring ){
        System.out.println(orderId+ "-------" +substring);
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("order_id",orderId);
        updateWrapper.set("tran_order_id",substring);
        return iPdOrderCargo.update(updateWrapper);
    }
}
